package jdktcp.client;

import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

public class JDKTcpClientForBrokenPipe {

    static int timeout = 1000 * 10;

    static String host = "localhost";
    static int port = 9999;

    public JDKTcpClientForBrokenPipe() {
        try (Socket client = new Socket()) {

            client.setSoTimeout(timeout);
            client.connect(new InetSocketAddress(host, port), timeout);
            System.out.println("start connect to:" + host + ":" + port);

            sendAndRevice(client);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void sendAndRevice(Socket client) throws Exception {
        System.out.println("connect:" + client);
        OutputStream out = client.getOutputStream();
        out.write(("HelloServer").getBytes());
        out.close();
    }

    public static void main(String[] args) throws Exception {
        new JDKTcpClientForBrokenPipe();
    }
}
